import gameobject
from gameobject import *

from pygame import *

import joshfunctions
from joshfunctions import *

import random

class Projectile(GameObject):
    def __init__(self, sprite, position, origin, damage, direction = 0.0, speed = 400.0):
        GameObject.__init__(self)

        #direction of the projectile, in radians
        self.direction = direction
        self.speed = speed
        self.position = position
        self.damage = damage
        self.sprite = sprite
        self.drawrect = Rect((0.0,0.0),(32.0,32.0))
        self.drawrect.center = self.position
        self.destroyed = False
        self.origin = origin
        #print "Created projectile at ", self.position
    def update(self, etime, enemies, players):
        currentx = self.position[0]
        currenty = self.position[1]

        currentx -= dx(self.speed*etime, self.direction)
        currenty -= dy(self.speed*etime, self.direction)
        self.position = (currentx, currenty)
        self.drawrect.center = self.position

        for enemy in enemies:
            #print self.drawrect.center, enemy.drawrect.center
            if dist(self.drawrect.center, enemy.drawrect.center) <= 50:
                if enemy.warping != True:
                    enemy.health -= self.damage
                    if enemy.health <= 0.0:
                        enemy.destroyed = True
                        print self.origin
                        players[self.origin].score += int((5+random.randint(0,5))*players[self.origin].combo)
                        players[self.origin].combo += .05
                    self.destroyed = True
        if (self.position[0] < -32 or self.position[0] > 1040) or (self.position[1] < -20  or self.position[1] > 600):
            #print self.position
            self.destroyed = True
    def draw(self, surf):
        surf.blit(self.sprite, self.drawrect.topleft)